//
//  MSSaleFilterViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/7.
//  Copyright © 2016年 Mike. All rights reserved.
//

#define BackViewHeight         (SCREEN_HEIGHT-KNavBarHeight-160)

#import "MSSaleFilterViewController.h"
#import "MSSaleFilterTableVC.h"

@interface MSSaleFilterViewController ()
@property UIView * backgroundView;
@property (nonatomic, assign) float positionY;
@property (nonatomic, assign) float bgHeight;
@property(nonatomic)UIView *tabbarCoverView;
//弹出的筛选列表的视图容器
@property UIView* contentView;
@property (nonatomic, strong) NSMutableArray    *mediumStateArray;    //状态
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, assign) BOOL selectTime;
@property (nonatomic, strong) MSSaleFilterTableVC *tableVC;
@end

@implementation MSSaleFilterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, 0.01, 0.01);
    _positionY = 160;
    _bgHeight = SCREEN_HEIGHT-KNavBarHeight-_positionY;
    
    //灰色背景
    self.backgroundView=[[UIView alloc]init];
    self.backgroundView.frame = CGRectMake(0, _positionY + KNavBarHeight, SCREEN_WIDTH, _bgHeight);
    self.backgroundView.backgroundColor=[UIColor colorWithWhite:0.f alpha:0.4f];
    self.backgroundView.clipsToBounds = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapclick)];
    tapGesture.delegate = (id)self;
    [self.backgroundView addGestureRecognizer:tapGesture];
    self.view.clipsToBounds=YES;
    self.view.backgroundColor=[UIColor clearColor];
    
    self.tableVC = [[MSSaleFilterTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.backgroundView addSubview:self.tableVC.view];
    
    self.mediumStateArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i< self.stateArray.count; i++){
        [self.mediumStateArray addObject:@"NO"];
    }
    _lastIndex = 0;
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self.tableVC.tableView reloadData];
}

- (void)showInView:(float)topY
{
    _positionY = topY;
    _bgHeight = SCREEN_HEIGHT-KNavBarHeight-_positionY;
    self.backgroundView.frame = CGRectMake(0, _positionY + KNavBarHeight, SCREEN_WIDTH, _bgHeight);
    
    _selectTime = NO;
    if (!self.backgroundView.superview) {
        [_backgroundView addSubview:self.view];
        UIWindow *window = [UIApplication sharedApplication].windows[0];
        [window addSubview:self.tabbarCoverView];
        [window addSubview:self.backgroundView];
    }
    for (int i = 0; i< self.mediumStateArray.count; i++){
        NSString *stateStr = [self.mediumStateArray[i] isEqualToString:@"YES"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:i withObject:stateStr];
    }
    self.tableVC.stateArray = self.mediumStateArray;
    self.tableVC.dataArray = [self.dataArray mutableCopy];
    self.tableVC.view.height = MIN(self.tableVC.tableView.contentSize.height, _bgHeight-160);
}

- (void)dismiss
{
    [self.tabbarCoverView removeFromSuperview];
    [self.backgroundView removeFromSuperview];
    [self.view removeFromSuperview];
}

- (void)tapclick{
    [self.tabbarCoverView removeFromSuperview];
    [self.backgroundView removeFromSuperview];
    [self.view removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(selectedFilterWithParmas: code:)]) {
        [self.delegate selectedFilterWithParmas:1000 code:@""];
    }
}

- (void)selectedTime
{
    _selectTime = YES;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
    [self tableView:self.tableVC.tableView didSelectRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *resultStr = self.dataArray[indexPath.row];
//    NSDictionary *dict = self.dataArray[indexPath.row];
//    if ([dict isKindOfClass:[NSDictionary class]]) {
//        resultStr = [HDUtil check:dict[@"name"]];
//    }
    if ([resultStr isEqualToString:@"自定义"] && _selectTime){
        if (_lastIndex != indexPath.row) {
            UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_lastIndex inSection:0]];
            lastCell.textLabel.textColor = kLightBlackColor;
            [self.stateArray replaceObjectAtIndex:_lastIndex withObject:@"NO"];
        }
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = [self.stateArray[indexPath.row] isEqualToString:@"NO"]?kBlueColor:kLightBlackColor;
        NSString *stateStr = [self.stateArray[indexPath.row] isEqualToString:@"NO"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:indexPath.row withObject:stateStr];
        
        _lastIndex = indexPath.row;
        
        [self callBackData];
    }
    if ([resultStr isEqualToString:@"自定义"] && !_selectTime){//显示时间控件
        if ([self.delegate respondsToSelector:@selector(selectedFilterWithParmas:code:)]) {
            [self.delegate selectedFilterWithParmas:100 code:@""];
        }
//        _lastIndex = indexPath.row;
    }
//其他点击
    if (![resultStr isEqualToString:@"自定义"]) {
        if (_lastIndex != indexPath.row) {
            UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_lastIndex inSection:0]];
            lastCell.textLabel.textColor = kLightBlackColor;
            [self.stateArray replaceObjectAtIndex:_lastIndex withObject:@"NO"];
        }
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = [self.stateArray[indexPath.row] isEqualToString:@"NO"]?kBlueColor:kLightBlackColor;
        NSString *stateStr = [self.stateArray[indexPath.row] isEqualToString:@"NO"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:indexPath.row withObject:stateStr];
        
        _lastIndex = indexPath.row;
        
        [self callBackData];
    }
}

- (void)callBackData{
    NSString *codeStr = _codeArray[_lastIndex];
    if ([self.delegate respondsToSelector:@selector(selectedFilterWithParmas:code:)]) {
        [self.delegate selectedFilterWithParmas:_lastIndex code:codeStr];
    }
    [self dismiss];
    for (int i = 0; i< self.stateArray.count; i++){
        NSString *stateStr = [self.stateArray[i] isEqualToString:@"YES"] ? @"YES":@"NO";
        [self.mediumStateArray replaceObjectAtIndex:i withObject:stateStr];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.backgroundView];
    return !CGRectContainsPoint(self.tableVC.view.frame, touchPoint);
}

//- (UIView *)tabbarCoverView
//{
//    if (!_tabbarCoverView) {
//        _tabbarCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENFRAME.size.height-KTabBarHeight, SCREEN_WIDTH, KTabBarHeight)];
//        _tabbarCoverView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4f];
//        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapclick)];
//        [_tabbarCoverView addGestureRecognizer:tapGesture];
//    }
//    return _tabbarCoverView;
//}

@end
